package com.txunda.zhuohired.adapter.user.home;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.aty.user.home.JobDetailsAty;
import com.txunda.zhuohired.bean.user.home.ReruitBean;

import java.util.ArrayList;


/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/2/27 9:18
 * 功能描述：
 * 联系方式：jiahuanchen@126.com
 */

public class JobListAdapter extends BaseAdapter {

    private Context context;
    private ViewHolder holder;
    private ArrayList<ReruitBean.DataBean> list = new ArrayList<>();

    public JobListAdapter(Context context) {
        this.context = context;
    }

    public void setData(ArrayList<ReruitBean.DataBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_home_list, null);
            holder = new ViewHolder();
            view.setTag(holder);
            ViewUtils.inject(holder, view);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        final ReruitBean.DataBean dataBean = list.get(i);
        holder.ll_item_home_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, JobDetailsAty.class);
                intent.putExtra("job_detail_id", dataBean.getId());
                context.startActivity(intent);
            }
        });
        if (TextUtils.isEmpty(dataBean.getTitle())) {
            holder.tv_item_home_list_name.setVisibility(View.INVISIBLE);
        } else {
            holder.tv_item_home_list_name.setVisibility(View.VISIBLE);
            holder.tv_item_home_list_name.setText(dataBean.getTitle());
        }
        Glide.with(context).load(dataBean.getLogo()).error(R.mipmap.icon_defult).placeholder(R.mipmap.icon_defult)
                .dontAnimate().into(holder.iv_item_home_list);
        if (TextUtils.isEmpty(dataBean.getPrice())) {
            holder.tv_item_home_list_pay.setVisibility(View.INVISIBLE);
        } else {
            holder.tv_item_home_list_pay.setVisibility(View.VISIBLE);
            holder.tv_item_home_list_pay.setText(dataBean.getPrice());
        }
        if (TextUtils.isEmpty(dataBean.getAddress()) && TextUtils.isEmpty(dataBean
                .getClaim_shooling()) && TextUtils.isEmpty(dataBean.getClaim_experience())) {
            holder.tv_item_home_ask.setVisibility(View.INVISIBLE);
        } else {
            holder.tv_item_home_ask.setVisibility(View.VISIBLE);
            holder.tv_item_home_ask.setText(dataBean.getAddress() + dataBean.getClaim_shooling()
                    + dataBean.getClaim_experience());
        }
        if (TextUtils.isEmpty(dataBean.getName())) {
            holder.tv_item_home_list_nickname.setVisibility(View.INVISIBLE);
        } else {
            holder.tv_item_home_list_nickname.setVisibility(View.VISIBLE);
            holder.tv_item_home_list_nickname.setText(dataBean.getName());
        }
        String string = dataBean.getWelfare();
        String[] stringArr = string.split(",");
        if (TextUtils.isEmpty(dataBean.getWelfare())) {
            holder.tv_item_home_list_welfare_one.setVisibility(View.GONE);
            holder.tv_item_home_list_welfare_two.setVisibility(View.GONE);
            holder.tv_item_home_list_welfare_three.setVisibility(View.GONE);
        } else if (stringArr.length == 1) {
            holder.tv_item_home_list_welfare_one.setVisibility(View.VISIBLE);
            holder.tv_item_home_list_welfare_one.setText(stringArr[0]);
            holder.tv_item_home_list_welfare_two.setVisibility(View.GONE);
            holder.tv_item_home_list_welfare_three.setVisibility(View.GONE);
        } else if (stringArr.length == 2) {
            holder.tv_item_home_list_welfare_one.setVisibility(View.VISIBLE);
            holder.tv_item_home_list_welfare_one.setText(stringArr[0]);
            holder.tv_item_home_list_welfare_two.setVisibility(View.VISIBLE);
            holder.tv_item_home_list_welfare_two.setText(stringArr[1]);
            holder.tv_item_home_list_welfare_three.setVisibility(View.GONE);
        } else if (stringArr.length == 3) {
            holder.tv_item_home_list_welfare_one.setVisibility(View.VISIBLE);
            holder.tv_item_home_list_welfare_one.setText(stringArr[0]);
            holder.tv_item_home_list_welfare_two.setVisibility(View.VISIBLE);
            holder.tv_item_home_list_welfare_two.setText(stringArr[1]);
            holder.tv_item_home_list_welfare_three.setText(stringArr[2]);
            holder.tv_item_home_list_welfare_three.setVisibility(View.VISIBLE);
        }
        if (TextUtils.isEmpty(dataBean.getPosition_good())) {
            holder.tv_item_home_list_introduce.setVisibility(View.GONE);
        } else {
            holder.tv_item_home_list_introduce.setVisibility(View.VISIBLE);
            holder.tv_item_home_list_introduce.setText(dataBean.getPosition_good());
        }
        return view;
    }

    class ViewHolder {
        @ViewInject(R.id.ll_item_home_list)
        private LinearLayout ll_item_home_list;
        @ViewInject(R.id.tv_item_home_list_name)
        private TextView tv_item_home_list_name;
        /**
         * 公司logo
         */
        @ViewInject(R.id.iv_item_home_list)
        private ImageView iv_item_home_list;

        @ViewInject(R.id.tv_item_home_ask)
        private TextView tv_item_home_ask;

        @ViewInject(R.id.tv_item_home_list_nickname)
        private TextView tv_item_home_list_nickname;

        @ViewInject(R.id.tv_item_home_list_pay)
        private TextView tv_item_home_list_pay;
        /**
         * 福利1
         */
        @ViewInject(R.id.tv_item_home_list_welfare_one)
        private TextView tv_item_home_list_welfare_one;
        /**
         * 福利2
         */
        @ViewInject(R.id.tv_item_home_list_welfare_two)
        private TextView tv_item_home_list_welfare_two;
        /**
         * 福利3
         */
        @ViewInject(R.id.tv_item_home_list_welfare_three)
        private TextView tv_item_home_list_welfare_three;

        @ViewInject(R.id.tv_item_home_list_introduce)
        private TextView tv_item_home_list_introduce;

    }

}
